
import UIKit
import SnapKit
public class GXLHUD: UIView{
    
    // MARK: 1.lift cycle
    
    
    /// 展示文字
    /// - Parameters:
    ///   - title: 内容
    ///   - afterDelay: 延迟关闭时间，默认 2秒
    ///   - textAlignment: 文字排布，默认居中
    ///   - completion: 完成事件
    /// - Returns:
    @discardableResult
    public class func show(_ title:String, _ afterDelay:TimeInterval = 2,_ textAlignment:NSTextAlignment = .center, completion:((Bool) -> Void)? = nil) -> GXLHUD{
        let noticeView = GXLHUD.init(frame: UIScreen.main.bounds)
        noticeView.setupUI()
        noticeView.labelTitle.text = title
        noticeView.labelTitle.textAlignment = textAlignment
        noticeView.labelTitle.lineBreakMode = .byWordWrapping;
        noticeView.labelTitle.numberOfLines = 0;
        noticeView.show(afterDelay: afterDelay)
        noticeView.completion = completion
        return noticeView
    }
    
    @discardableResult
    public class func show(_ title:String, image:UIImage?, _ afterDelay:TimeInterval = 2 , completion:((Bool) -> Void)? = nil) -> GXLHUD{
        let noticeView = GXLHUD.init(frame: UIScreen.main.bounds)
        noticeView.setupImageUI()
        noticeView.labelTitle.text = title
        noticeView.imageView.image = image
        noticeView.show(afterDelay: afterDelay)
        noticeView.completion = completion
        return noticeView
    }
    
    // MARK: 2.private methods
    private func show(afterDelay: TimeInterval)  {
        let window = UIApplication.shared.keyWindow!
        window.addSubview(self)
        self.snp.makeConstraints { (maker) in
            maker.edges.equalToSuperview()
        }
        UIView.animate(withDuration: 0.3, animations: {
            self.contentView.layer.opacity = 1.0
        }) { (finished) in
            self.perform(#selector(self.remove), with: nil, afterDelay: afterDelay)
        }
    }
    
    @objc private func remove(){
        UIView.animate(withDuration: 0.3, animations: {
            self.contentView.layer.opacity = 0
        }) { (finished) in
            self.removeFromSuperview()
            self.completion?(true)
        }
    }
    // MARK: 3.event response
    private func setupUI(){
        contentView.snp.makeConstraints { (maker) in
            maker.left.greaterThanOrEqualTo(50)
            maker.right.lessThanOrEqualTo(-50)
            maker.center.equalToSuperview()
        }
        
        labelTitle.snp.makeConstraints { (maker) in
            maker.left.equalTo(28)
            maker.right.equalTo(-28)
            maker.top.equalTo(14)
            maker.bottom.equalTo(-14)
        }
    }
    
    private func setupImageUI(){
        contentView.snp.makeConstraints { (maker) in
            maker.left.greaterThanOrEqualTo(50)
            maker.right.lessThanOrEqualTo(-50)
            maker.center.equalToSuperview()
        }
        
        imageView.snp.makeConstraints { (maker) in
            maker.centerX.equalToSuperview()
            maker.top.equalTo(16)
        }
        
        labelTitle.snp.makeConstraints { (maker) in
            maker.left.equalTo(28)
            maker.right.equalTo(-28)
            maker.top.equalTo(imageView.snp.bottom).offset(14)
            maker.bottom.equalTo(-14)
        }
    }
    // MARK: 4.interface
    var completion: ((Bool) -> Void)? = nil
    // MARK: 5.getter
    private lazy var labelTitle: UILabel = {
        let labelTitle = UILabel()
        labelTitle.textColor = .white
        labelTitle.font = UIFont.systemFont(ofSize: 16)
        labelTitle.numberOfLines = 0
        labelTitle.textAlignment = .center
        contentView.addSubview(labelTitle)
        return labelTitle
    }()
    
    private lazy var contentView: UIView = {
        let contentView = UIView()
        contentView.layer.cornerRadius = 5
        contentView.backgroundColor = UIColor.init(white: 0, alpha: 0.7)
        contentView.layer.masksToBounds = true
        addSubview(contentView)
        return contentView
    }()
    
    private lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = UIView.ContentMode.scaleAspectFit
        contentView.addSubview(imageView)
        return imageView
    }()
}
